package _03字节流复制文件的4种方式;

import java.io.*;

public class CopyDemo {
    public static void main(String[] args) throws IOException {
//        test01();
//        test02();
//        test03();
        test04();
    }

    //基本流读读写一个数据  46643ms
    public static void test01() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("day11\\abd\\dnf.mp4");
        FileOutputStream fos = new FileOutputStream("day11\\abd\\movie\\dnf.mp4");
        int read;
        while ((read=fis.read())!=-1){
            fos.write(read);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
    //基本流读读写一个数据    73ms
    public static void test02() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("day11\\abd\\dnf.mp4");
        FileOutputStream fos = new FileOutputStream("day11\\abd\\movie\\dnf.mp4");
        int read;
        byte[] bytes = new byte[1024];
        while ((read=fis.read(bytes))!=-1){
            fos.write(bytes,0,read);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    //缓冲读读写一个数据    145ms
    public static void test03() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("day11\\abd\\dnf.mp4");
        FileOutputStream fos = new FileOutputStream("day11\\abd\\movie\\dnf.mp4");

        BufferedInputStream bis =new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        int read;
        while ((read=bis.read())!=-1){
            bos.write(read);
        }
        bos.close();
        bis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    //缓冲读读写一组数据    23ms
    public static void test04() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("day11\\abd\\dnf.mp4");
        FileOutputStream fos = new FileOutputStream("day11\\abd\\movie\\dnf.mp4");

        BufferedInputStream bis =new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int read;
        byte[] bytes = new byte[1024];
        while ((read=bis.read(bytes))!=-1){
            bos.write(bytes,0,read);
        }
        fos.close();
        fis.close();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}
